﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using AutoCorrelatorGUI.Model;
using AutoCorrelatorGUI.Properties;
using AutoCorrelatorGUI.Utility;
using AutoCorrelatorGUI.ViewModel.Messages;
using Common.Utility;
using Common.ViewModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using OxyPlot;
using OxyPlot.Series;

namespace AutoCorrelatorGUI.ViewModel
{
   /// <summary>
   /// This class contains properties that a View can data bind to.
   /// <para>
   /// See http://www.galasoft.ch/mvvm
   /// </para>
   /// </summary>
   public class InputDisplayViewModel : ViewModelBase
   {
      /// <summary>
      /// Initializes a new instance of the MvvmViewModel1 class.
      /// </summary>
      public InputDisplayViewModel()
      {
         Messenger.Default.Register<FullRefreshMessage>(this, OnFullRefresh);

         Style headerColorStyle = new Style(typeof(TextBlock));
         headerColorStyle.Setters.Add(new Setter(TextBlock.BackgroundProperty, new Binding("Color") { Converter = new ColorToBrushConverter() }));

         InputDataColumnCollection = new ObservableCollection<DataGridColumn>();
         InputDataColumnCollection.Add(new DataGridTextColumn() { Header = "ID", Binding = new Binding("Title"), ElementStyle = headerColorStyle });

         //Request any existing data
         Messenger.Default.Send(new RequestDataMessage(OnFullRefresh));
      }

      private void OnFullRefresh(FullRefreshMessage message)
      {
         InternalWellData = message.DataView.SourceCollection as MultiSelectBindingList<WellVM>;
         InternalWellDataView = message.DataView;

         //Find the min and max range
         int maxDataCount = 0;

         for (int i = 0; i < InternalWellData.Count; i++)
         {
            maxDataCount = Math.Max(maxDataCount, (InternalWellData[i] as WellVM).Input.Data.Count);
         }

         while (InputDataColumnCollection.Count - 1 > maxDataCount)
         {
            InputDataColumnCollection.RemoveAt(InputDataColumnCollection.Count - 1);
         }

         for (int i = InputDataColumnCollection.Count - 1; i < maxDataCount; i++)
         {
            var col = new DataGridTextColumn() { Header = i.ToString(), Binding = new Binding("Input.Data[" + i.ToString() + "].Y") { StringFormat = "G8" }, CanUserSort = true, SortMemberPath = "Input.Data[" + i.ToString() + "].Y" };
            CustomSort.SetCustomSorter(col, new WellBaseComparer());
            InputDataColumnCollection.Add(col);
         }
      }

      private MultiSelectBindingList<WellVM> _InternalWellData = null;

      public MultiSelectBindingList<WellVM> InternalWellData
      {
         get { return _InternalWellData; }
         set
         {
            if (_InternalWellData == value)
            {
               return;
            }

            _InternalWellData = value;
            RaisePropertyChanged("InternalWellData");
         }
      }

      private ListCollectionView _InternalWellDataView = null;

      public ListCollectionView InternalWellDataView
      {
         get
         {
            return _InternalWellDataView;
         }

         set
         {
            if (_InternalWellDataView == value)
            {
               return;
            }

            _InternalWellDataView = value;
            RaisePropertyChanged("InternalWellDataView");
         }
      }

      public ObservableCollection<DataGridColumn> InputDataColumnCollection
      {
         get;
         private set;
      }
   }
}
